//应收结算/应收账单生成

/**
 * 应收结算列表视图
 */
interface ReceivableCloseBillViewModel extends IBaseViewModel<ReceivableCloseBillQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceReceivableListItemResponse>;
    /**
     *应收总额
     */
    receivableTotalMoney: number;
    /**
     *结算人
     */
    reckonerName: string;
    /**
     *应收说明
     */
    description: string;
    /**
     *生成应收账单
     */
    addReceivable: () => void;
    /**
     *删除子单
     */
    DelId: (id: string) => void;

    back:()=>void;
    

}

interface ReceivableCloseBillQueryTerms {

}


class ReceivableCloseBillController extends ListController<ReceivableCloseBillViewModel, ReceivableCloseBillQueryTerms> {
    gridApi: uiGrid.IGridApiOf<FinanceReceivableListItemResponse>;

    /**
     *从应收结算列表传递过来的每个应收编号(code)组成的数组
     */
    fixReceCode: string[];
    /**
     *从应收结算列表传递过来的每个应收编号标识符(id)组成的数组
     */
    fixReceId: string[];

    receivableIdList: any;


    skip: number;

    count: number;

    constructor($scope: ReceivableCloseBillViewModel,
        private receivableService: IReceivableService,
        private valueService: IValueService,
        private $ngBootbox: BootboxService,
        private $state: angular.ui.IStateService,
        private $http: ng.IHttpService,
        private $location: ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.fixReceCode = this.$location.search().id.split(",");
        this.fixReceId = this.$location.search().name.split(",");
        
        if(window.localStorage.getItem('loginData')){
             this.$scope.reckonerName=JSON.parse(window.localStorage.getItem('loginData')).realName;
        }

        this.$scope.receivableTotalMoney = 0;
        this.$scope.description = "";


        this.getSumreceivable(this.fixReceId.join(","));
        this.$scope.addReceivable = this.addReceivable;
        this.$scope.DelId = (id)=>{this.DelId(id,this.$scope.gridOptions.data)};

        this.$scope.back = this.back;

        this.$scope.gridOptions = {
            data: [],
            enableSelectAll: false,//全选禁用
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            enableSorting: false,
            multiSelect: true,
            columnDefs: [
                {
                    displayName: "应收编号", field: 'receivableCode', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: "应收费用类型", field: 'feeType', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: "应收说明", field: 'receivableSummary', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '客户单位', field: 'client', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '结算单位', field: 'settle', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '送货地址', field: 'originAddress', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '发货日期', field: 'realDeliveryTime', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '发货时间', field: 'realDeliveryTime', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '应收总价', field: 'receivableTotalPrice', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '合同号', field: 'contractNumber', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '结算状态', field: 'settleStatus', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: '回单状态', field: 'receiptStatus', enableColumnMenu: false, cellTooltip:
                    true, headerTooltip: true,
                },
                {
                    displayName: "操作",
                    field: 'orderStatus',
                    cellTemplate: '<a class="primary fa fa-trash-o text-danger m-l-xs" title="删除" ng-click="grid.appScope.DelId(row.entity.id)"></a>', enableColumnMenu: false, enableSorting: false
                }],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        }

        this.query();
    }


    //加载数据
    loadData(queryTerms: ReceivableCloseBillQueryTerms, skip: number, count: number): void {
        var receArr = [];
        this.fixReceCode.forEach(function (item, index) {
            receArr.push(item);
        });
        this.getDataLise(receArr.splice(skip, count));
    }
    
    getDataLise = (list)=> {
        var receivableIdList;
        this.receivableService.receivablelist("", true, "", "", "", "", "", "", "", "", "", "", "", "", "", true, list, "", false, "", "", "","", "","", 0, -1).then((res) => {
            this.$scope.gridOptions.data = res.data.data;
            this.$scope.gridOptions.totalItems = this.fixReceCode.length;
            receivableIdList = this.fixReceCode;
        })
    }


    /**
     * 计算应收总额
     */
    getSumreceivable = (idList: string) => {
        this.receivableService.sumreceivable(idList).then((res) => {
            this.$scope.receivableTotalMoney = res.data.totalReceivable;
        }, (rej) => { })
    }

    /**
     * 生成应收账单
     */
    addReceivable = () => {
        this.$ngBootbox.confirm("确定生成应收账单吗?").then(() => {
            this.receivableService.addReceivableSettleBill(this.$scope.description, this.fixReceId).then((res) => {
                this.$state.go("app.receivable.receivableClose",{name:'receivableCodeList'})
            }, (rej) => { })
        }, () => { return })
    }


    isFinanceReceivableListItemResponse(response: string | FinanceReceivableListItemResponse[]): response is FinanceReceivableListItemResponse[] {
        return (<FinanceReceivableListItemResponse[]>response).forEach !== undefined;
    }

    /**
     * 删除子单
     */
    DelId = (id: string,data:string | FinanceReceivableListItemResponse[]) => {
        this.$ngBootbox.confirm("确定删除子单吗?").then(() => {
            if (this.isFinanceReceivableListItemResponse(data)) {
                data.forEach((item, index) => {
                    if (item.id == id) {
                        if (this.isFinanceReceivableListItemResponse(data)) {
                            data.splice(index, 1);
                        }
                        this.fixReceId.forEach((items, index) => {
                            if (item.id == items) {
                                this.fixReceId.splice(index, 1);
                                this.fixReceCode.splice(index, 1);
                                var receArr = [];
                                this.fixReceCode.forEach((item, index) => {
                                    receArr.push(item);
                                });
                                if (receArr.length < 1) {
                                    this.$state.go("app.receivable.receivableClose",{name:'receivableCodeList'})
                                    return;
                                }
                                this.getDataLise(receArr.splice(0, 10));
                                this.getSumreceivable(this.fixReceId.join(","));
                                this.$scope.gridOptions.paginationCurrentPage = 1;
                            }
                        })
                    }
                })
            }
        }, () => { return })
    }


    //返回
    back=()=>{
        this.$state.go("app.receivable.receivableClose",{name:'receivableCodeList'})
    }
}

angular.module("tms").controller("receivableCloseBillController", ["$scope",
    "receivableService",
    "valueService",
    "$ngBootbox",
    "$state",
    "$http",
    "$location",
    ReceivableCloseBillController]);